﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XTextsJig : XUiJigBase
	{
		public XTextsJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XTextsJig.EStage.eNoOper;
		}

		public XYZ DragTexts(List<TextNote> lstText, List<XYZ> lstTextCenterPt, XYZ ptBase, string strPrompt = "请确定位置：")
		{
			try
			{
				this.TextNotes = lstText;
				this.TextNoteCenter = lstTextCenterPt;
				this.BasePoint = ptBase;
				this.JigStage = XTextsJig.EStage.ePickPoint;
				this.PickPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.PickPoint = null;
			}
			this.JigStage = XTextsJig.EStage.eNoOper;
			return this.PickPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.m_ptCur.IsEmpty || this.JigStage != XTextsJig.EStage.ePickPoint;
			if (!flag)
			{
				bool flag2 = this.TextNotes.Count != this.TextNoteCenter.Count;
				if (!flag2)
				{
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					XYZ xyz2 = xyz - this.BasePoint;
					Transform transform = Transform.CreateTranslation(xyz2);
					Graphics graphics = paintEventArgs.Graphics;
					Brush black = Brushes.Black;
					StringFormat stringFormat = new StringFormat();
					stringFormat.Alignment = StringAlignment.Center;
					stringFormat.LineAlignment = StringAlignment.Center;
					try
					{
						System.Drawing.Point ptStart = base.Revit2Client(this.BasePoint);
						XGraphics g = new XGraphics(graphics, base.JigPen);
						XLine2D xline2D = new XLine2D(ptStart, this.m_ptCur);
						xline2D.Draw(g);
						for (int i = 0; i < this.TextNotes.Count; i++)
						{
							TextNote textNote = this.TextNotes[i];
							XYZ xyz3 = this.TextNoteCenter[i];
							xyz3 = transform.OfPoint(xyz3);
							System.Drawing.Point point = base.Revit2Client(xyz3);
							PointF point2 = new PointF((float)point.X, (float)point.Y);
							float textHeight = this.GetTextHeight(textNote, xyz3);
							string textStyle = this.GetTextStyle(textNote);
							Font font = new Font(textStyle, textHeight, GraphicsUnit.Pixel);
							Matrix transform2 = graphics.Transform;
							Matrix transform3 = graphics.Transform;
							float textAngle = this.GetTextAngle(textNote);
							transform3.RotateAt(textAngle, point2);
							float num = (float)textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
							transform3.Scale(num, 1f);
							graphics.Transform = transform3;
							point2.X /= num;
							graphics.DrawString(textNote.Text, font, black, point2, stringFormat);
							graphics.Transform = transform2;
							font.Dispose();
						}
					}
					catch
					{
					}
				}
			}
		}

		private string GetTextStyle(TextNote txtnote)
		{
			string name = txtnote.TextNoteType.Name;
			bool flag = name.IndexOf("仿宋") >= 0;
			string result;
			if (flag)
			{
				result = "仿宋";
			}
			else
			{
				bool flag2 = name.IndexOf("宋体") >= 0;
				if (flag2)
				{
					result = "宋体";
				}
				else
				{
					bool flag3 = name.IndexOf("黑体") >= 0;
					if (flag3)
					{
						result = "黑体";
					}
					else
					{
						bool flag4 = name.IndexOf("Arial") >= 0;
						if (flag4)
						{
							result = "Arial";
						}
						else
						{
							result = "宋体";
						}
					}
				}
			}
			return result;
		}

		private float GetTextHeight(TextNote txtnote, XYZ ptTextCenter)
		{
			double num = txtnote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
			double height = txtnote.Height;
			double num2 = 0.5 * (num + height) * (double)base.UiDoc.Document.ActiveView.Scale;
			XYZ point = ptTextCenter + base.UiDoc.Document.ActiveView.UpDirection * 0.5 * num2;
			System.Drawing.Point point2 = base.Revit2Client(ptTextCenter);
			System.Drawing.Point point3 = base.Revit2Client(point);
			double num3 = (double)Math.Abs(point2.X - point3.X);
			double num4 = (double)Math.Abs(point2.Y - point3.Y);
			double num5 = Math.Sqrt(num3 * num3 + num4 * num4);
			return (float)(2.0 * num5);
		}

		private float GetTextAngle(TextNote txtnote)
		{
			Autodesk.Revit.DB.View activeView = base.UiDoc.Document.ActiveView;
			double num = activeView.RightDirection.AngleOnPlaneTo(txtnote.BaseDirection, activeView.ViewDirection);
			bool flag = num <= Math.PI*.5;
			if (!flag)
			{
				bool flag2 = num <= 4.71238898038469;
				if (flag2)
				{
					num -= Math.PI;
				}
				else
				{
					bool flag3 = num <= Math.PI*2;
					if (flag3)
					{
						num -= Math.PI*2;
					}
				}
			}
			float num2 = (float)(num / Math.PI * 180.0);
			return -num2;
		}

		private XYZ GetTextCenter(TextNote txtnote)
		{
			BoundingBoxXYZ boundingBoxXYZ = txtnote.get_BoundingBox(base.UiDoc.ActiveView);
			XYZ min = boundingBoxXYZ.Min;
			XYZ max = boundingBoxXYZ.Max;
			return min + (max - min) / 2.0;
		}

		public List<TextNote> TextNotes { get; set; }

		public List<XYZ> TextNoteCenter { get; set; }

		public XYZ BasePoint { get; set; }

		public XYZ PickPoint { get; set; }

		public XTextsJig.EStage JigStage { get; set; }

		public enum EStage
		{
			eNoOper,
			ePickPoint
		}
	}
}
